﻿using Dapper;
using Entity;
using IRepository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace Repository
{
    public class OrganizationRepository : IOrganizationRepository
    {
        // 注入数据库连接（由依赖注入容器提供，已配置连接字符串）
        private readonly IDbConnection _dbConnection;

        public OrganizationRepository(IDbConnection dbConnection)
        {
            _dbConnection = dbConnection;
        }

        /// <summary>
        /// 获取所有未删除的组织列表
        /// </summary>
        /// <returns>组织列表（未删除）</returns>
        public async Task<List<SysOrgOrganizations>> GetAllAsync()
        {
            string sql = @"
                SELECT 
                    organization_id, organization_name, sorting, status, 
                    create_by, create_time, update_by, update_time, 
                    is_delete, org_code, parent_organization_id
                FROM sys_org_organizations
                WHERE is_delete = '0'
                ORDER BY sorting ASC, create_time DESC";

            var result = await _dbConnection.QueryAsync<SysOrgOrganizations>(sql);
            return result.ToList();
        }
        /// <summary>
        /// 获取所有未删除的组织列表（修正方法名，与接口匹配）
        /// </summary>
        /// <returns>组织列表（未删除）</returns>
        public async Task<List<SysOrgOrganizations>> GetAllNotDeletedAsync() // 方法名修改为与接口一致
        {
            string sql = @"
                SELECT 
                    organization_id, organization_name, sorting, status, 
                    create_by, create_time, update_by, update_time, 
                    is_delete, org_code, parent_organization_id
                FROM sys_org_organizations
                WHERE is_delete = '0'
                ORDER BY sorting ASC, create_time DESC";

            var result = await _dbConnection.QueryAsync<SysOrgOrganizations>(sql);
            return result.ToList();
        }
    }
}